package hausaufgabe04;

import java.io.*;
import java.util.*;

import de.rwth.matse.dragon.*;

public class Goldmine
{
  public static Field feld;
  public static String monitor = "";
  public static boolean flag = false;
  
  class MineWork implements Runnable
  {
    private Dragon d;
    
    public MineWork(Dragon d)
    {
      this.d = d;
    }
    public void run()
    {
      while(d.canMove() || d.canTake())
      {
        if(d.canTake())
        {
          d.take();
          d.turnLeft();
          d.turnLeft();
        }
        if(d.canMove())
          d.move();
        if(feld.get(11, 4).contains(d) && d.getDirection().equals(Direction.RIGHT))
        {
          synchronized(monitor)
          {
            d.move();
            d.put();
            d.turnLeft();
            d.turnLeft();
            d.move();
            flag = true;
            monitor.notifyAll();
          }
        }
      }
    }
  }
  
  class OutsideWork implements Runnable
  {
    private Dragon d;
    
    public OutsideWork(Dragon d)
    {
      this.d = d;
    }
    public void run()
    {
      while(true)
      {
        synchronized(monitor)
        {
          if(!flag)
          {
            try
            {
              monitor.wait();
            }
            catch (InterruptedException e)
            {
              // TODO Automatisch erstellter Catch-Block
              e.printStackTrace();
            }
          }
          if(!flag)
            continue;
          d.move();
          if(d.canTake())
          {
            d.take();
            flag = false;
            if(d.getDirection().equals(Direction.DOWN))
              d.turnLeft();
            else
            {
              d.turnLeft();
              d.turnLeft();
              d.turnLeft();
            }
            d.move();
            monitor.notifyAll();
          }
          else
          {
            d.turnLeft();
            d.turnLeft();
            d.move();
            d.turnLeft();
            d.turnLeft();
            continue;
          }
        }
        while(d.canMove())
          d.move();
        d.turnLeft();
        while(d.canMove())
          d.move();
        d.put();
        d.turnLeft();
        d.turnLeft();
        d.move();
        d.move();
        if(feld.get(12, 3).isEmpty())
        {
          d.turnLeft();
          d.turnLeft();
          d.turnLeft();
          d.move();
          d.turnLeft();
        }
        else
        {
          d.move();
          d.move();
          d.turnLeft();
          d.turnLeft();
          d.turnLeft();
          d.move();
          d.turnLeft();
          d.turnLeft();
          d.turnLeft();
        }
      }
    }
  }
  
  public static void main(String[] args) throws FileNotFoundException
  {
    feld = Field.read(new DragonFieldReader(new Scanner(new File("Goldmine.txt"))));
    Dragon mineWorker1 = new Dragon(10, 4, Direction.LEFT, 0, feld);
    Dragon mineWorker2 = new Dragon(9, 4, Direction.LEFT, 0, feld);
    Dragon outsideWorker1 = new Dragon(12, 3, Direction.DOWN, 0, feld);
    Dragon outsideWorker2 = new Dragon(12, 5, Direction.UP, 0, feld);
    
    Goldmine gm = new Goldmine();
    
    Thread mineThread1 = new Thread(gm.new MineWork(mineWorker1));
    Thread mineThread2 = new Thread(gm.new MineWork(mineWorker2));
    Thread outsideThread1 = new Thread(gm.new OutsideWork(outsideWorker1));
    Thread outsideThread2 = new Thread(gm.new OutsideWork(outsideWorker2));
    
    mineThread1.start();
    mineThread2.start();
    outsideThread1.start();
    outsideThread2.start();
  }
}
